<?php
class main extends spController
{
	function __construct()
	{
		parent::__construct();
		if ($_COOKIE['uid']) {
			$usercookie['uid'] = $_COOKIE['uid'];
			$usercookie['username'] = $_COOKIE['username'];
			$this->usercookie = $usercookie;
		}
	}
	function index()
	{
		if ($this->spArgs('cateid')) {
			$option = array('cateid'=>$this->spArgs('cateid'));
		} else {
			$option = null;
		}
		$article = spClass("cms_article"); 	
		$article->linker['cms_article_data']['enabled'] = false;
		$results = $article->spLinker()->spPager($this->spArgs('page', 1), 20)->findAll($option, 'tid DESC');
		$this->pager = $article->spPager()->getPager();
		if ($results) {
			foreach ($results as $key=>$value) {
				$results[$key]['pubtime'] = date("Y-m-d H:i:s", $value['pubtime']);
				if (!$value['cate']) {
					$results[$key]['cate']['cateid'] = 0;
					$results[$key]['cate']['catename'] = '默认分类';
				}
			}
		}
		$this->results = $results;
		$this->display("index.html");
	}
	
	function post()
	{
		if ($this->spArgs('submit')) {
			//初始化模型类
			$tags    = spClass("cms_tags");
			$tagsLog = spClass("cms_tagslog");
			$article = spClass("cms_article");
			$articleData = spClass("cms_article_data");
			if ($this->spArgs('tag')) {
				
				$tag = '';
				$tagArr = $artTagArr = explode(' ', trim($this->spArgs('tag')));
				
				//处理标签数据
				if ($this->spArgs('tid')) {
					$condition = array('tid'=>trim($this->spArgs('tid')));
					$result = $article->find($condition);
					$nowTagArr = explode(',', $result['tag']);
					$num = count($nowTagArr);
					$nowTagArr[$num] = $nowTagArr[0];
					unset($nowTagArr[0]);
					$nowTagArrFlip = $delTagArr = array_flip($nowTagArr);
					foreach ($tagArr as $key=>$value) {
						if ($nowTagArrFlip[$value]) {
							unset($tagArr[$key]);
							unset($delTagArr[$value]);
						}
					}
				}
				
				//组合文章标签
				if ($artTagArr) {
					foreach ($artTagArr as $key=>$value) {
						if ($key == 5) {
							break;
						}
						if(!empty($value)) $tag .= $value.',';
					}
					$tag = substr($tag, 0, -1);
				}
				
				//编辑时，由于某些标签修改，需要将替换掉的标签从标签记录中删除和更新标签表
				if ($delTagArr) {
					foreach (array_flip($delTagArr) as $key=>$value) {
						//删除标签
						$tagsRow = $tags->find(array('tagname'=>$value));
						if ($tagsRow['tagnum'] == 1) {
							$status = $tags->delete(array('id'=>$tagsRow['id']));
						} else {
							$status = $tags->update(array('id'=>$tagsRow['id']), array('tagnum'=>$tagsRow['tagnum']-1));
						}
						//删除标签记录
						$status = $tagsLog->delete(array('tagid'=>$tagsRow['id'], 'tid'=>$this->spArgs('tid')));
					}
				}
			}
			
			//插入文章
			if ($this->spArgs('tid')) {
				$artUpadeRow = array(
					'title'   => $this->spArgs('title'),
					'cms_article_data' => array('content' => $this->spArgs('content')),
					'cateid'  => $this->spArgs('cateid'),
					'tag'     => $tag
				);
				$status = $article->spLinker()->update(array('tid'=>$this->spArgs('tid')), $artUpadeRow);
				$lastId = $this->spArgs('tid');
			} else {
				//插入主表
				if ($tag) {
					$newRow  = array(
						'title'   => $this->spArgs('title'),
						'cateid'  => $this->spArgs('cateid'),
						'cms_article_data' => array('content' => $this->spArgs('content')),
						'tag'     => $tag,
						'pubtime' => time(),
						'uid'     => $_COOKIE['uid']
					);
				} else {
					$newRow  = array(
						'title'   => $this->spArgs('title'),
						'cateid'  => $this->spArgs('cateid'),
						'cms_article_data' => array('content' => $this->spArgs('content')),
						'pubtime' => time(),
						'uid'     => $_COOKIE['uid']
					);
				}
				$lastId = $article->spLinker()->create($newRow);
			}
			
			//插入标签记录
			if ($tagArr) {
				foreach ($tagArr as $key=>$value) {
					if ($key == 5) {
						break;
					}
					$searchArr = array('tagname'=>$value);
					$tagsRow = $tags->find($searchArr);
					if ($tagsRow) {
						$updateArr = array('tagnum'=>$tagsRow['tagnum']+1);
						$tagsId  = $tags->update(array('id'=>$tagsRow['id']), $updateArr);
						$tagsLogId = $tagsLog->create(array('tagid'=>$tagsRow['id'], 'tid'=>$lastId));
					} else {
						$insertArr = array('tagname'=>$value, 'tagnum'=>1);
						$tagsId = $tags->create($insertArr);
						$tagsLogId = $tagsLog->create(array('tagid'=>$tagsId, 'tid'=>$lastId));
					}
					if (!$tagsId || !$tagsLogId) {
						if ($this->spArgs('tid')) {
							die('更新错误,程序已经中断');
						} else {
							die('发布错误,程序已经中断');
						}
					}
				}
			}
			if ($this->spArgs('tid')) {
				if ($status) {
					echo '更新成功';
				} else {
					echo '更新失败';
				}
			} else {
				if ($lastId) {
					echo '发布成功';
				} else {
					echo '发布失败';
				}
			}
			
		} else {
			$cate = spClass("cms_cate");
			$results = $cate->findAll();
			if (!$results) {
				$results[] = array('cateid'=>0, 'catename'=>'默认分类');
			}
			$this->results = $results;
			$this->display("post.html");
		}
	}
	
	function view()
	{
		$tid = $this->spArgs("tid");
		$article = spClass("cms_article");
		$condition = array('tid'=>$tid);
		$result = $article->spLinker()->find($condition);
		$result['pubtime'] = date("Y.m.d", $result['pubtime']);
		$tags = '';
		if ($result['tag']) {
			foreach (explode(',', $result['tag']) as $key=>$value) {
				$tags .= '<a href=\'s.php?tag='.$value.'\'>'.$value.'</a>';
			}
		}
		$this->tags   = $tags;
		if (!$result['cms_cate']) {
			$result['cms_cate'] = array('cateid'=>0, 'catename'=>'默认分类');
		}
		$this->result = $result;
		//更新阅读次数
		$updateRow = array('totalnum'=>$result['totalnum'] + 1);
		if ($result['today'] != date("Y-m-d")) {
			$updateRow['todaynum'] = 1;
			$updateRow['today']    = date("Y-m-d");
		} else {
			$updateRow['todaynum'] = $result['todaynum'] + 1;
		}
		$article->update(array('tid'=>$tid), $updateRow);
		$this->display("view.html");
	}
	
	function delete()
	{
		$article = spClass("cms_article");
		$tags = spClass("cms_tags");
		$tagsLog = spClass("cms_tagslog");
		$tid = $this->spArgs("tid");
		$condition = array('tid'=>$tid);
		$result = $article->find($condition);
		if ($result) {
			$tagArr = explode(',', $result['tag']);
			if ($tagArr) {
				foreach ($tagArr as $key=>$value) {
					$tagsArr = $tags->find(array('tagname'=>$value));
					if ($tagsArr['tagnum'] == 1) {
						$status = $tags->delete(array('id'=>$tagsArr['id']));
					} else {
						$status = $tags->update(array('id'=>$tagsArr['id']), array('tagnum'=>$tagsArr['tagnum']-1));
					}
					//删除标签记录
					$status = $tagsLog->delete(array('tagid'=>$tagsArr['id'], 'tid'=>$this->spArgs('tid')));
				}
			}
			$article->linker['cms_cate']['enabled'] = false;
			$tid = $article->spLinker()->delete($condition);
			if ($tid) {
				$ajaxResult = array('status' => true, 'message' => '删除成功');
			} else {
				$ajaxResult = array('status' => false, 'message' => '删除失败');
			}
		} else {
			$ajaxResult = array('status' => false, 'message' => '删除失败');
		}
		echo json_encode($ajaxResult);
	}
	
	function edit()
	{
		$cate = spClass("cms_cate");
		$results = $cate->findAll();
		if (!$results) {
			$results[] = array('cateid'=>0, 'catename'=>'默认分类');
		}
		$this->results = $results;
		$tid = $this->spArgs("tid");
		$article = spClass("cms_article");
		$condition = array('tid'=>$tid);
		$result = $article->spLinker()->find($condition);
		$result['tag'] = str_replace(',', ' ', $result['tag']);
		$this->result = $result;
		$this->display("post.html");
	}
	
	function tishi404()
	{
		$this->display("404.html");
	}
}